package com.xxx.lib.widget

import android.app.Dialog
import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.View
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ScreenUtils
import com.xxx.lib.databinding.DialogHintBinding

/**
 * 提示dialog
 */
class HintDialog(context: Context) : Dialog(context), View.OnClickListener {
    companion object {
        /**
         * 左边按钮点击回调
         */
        const val ONCLICK_LEFT = 100

        /**
         * 右边按钮点击回调
         */
        const val ONCLICK_RIGHT = 200

        /**
         * 单按钮点击回调
         */
        const val ONCLICK_CENTER = 300
    }

    private var title: CharSequence? = "温馨提示"
    private var leftText: CharSequence? = "取消"
    private var rightText: CharSequence? = "确定"
    private var centerText: CharSequence? = "确定"
    private var centerButtonVisiblity = false

    private var mCallBack: HintDialogCallBack? = null

    private val binding by lazy { DialogHintBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        setContentView(binding.root)
        window?.run {
            attributes?.gravity = Gravity.TOP
            attributes?.width = ConvertUtils.dp2px(280F)
            attributes?.y = ScreenUtils.getScreenHeight() / 4
            setBackgroundDrawable(ColorDrawable())
        }


        binding.btLeft.setOnClickListener(this)
        binding.btRight.setOnClickListener(this)
        binding.btCenter.setOnClickListener(this)

        setTitle(this.title)
        setButtonTextLeftRight(leftText, rightText)
        setButtonTextCenter(centerText)
        setCenterButtonVisiblity(centerButtonVisiblity)
    }


    /**
     * 设置标题
     * @param title 标题
     */
    override fun setTitle(title: CharSequence?) {
        this.title = title
        binding.tvTitle.text = title
    }

    /**
     * 设置提示
     * @param hint 提示
     */
    fun setHint(hint: String) {
        binding.tvHint.text = hint
    }

    /**
     * 设置左右按钮文本
     * @param leftText 左按钮文本
     * @param rightText 右按钮文本
     */
    fun setButtonTextLeftRight(leftText: CharSequence?, rightText: CharSequence?) {
        setButtonTextLeft(leftText)
        setButtonTextRight(rightText)
    }

    /**
     * 设置左按钮文本
     * @param leftText 左按钮文本
     */
    fun setButtonTextLeft(leftText: CharSequence?) {
        this.leftText = leftText
        binding.btLeft.text = leftText
    }

    /**
     * 设置右按钮文本
     * @param rightText 右按钮文本
     */
    fun setButtonTextRight(rightText: CharSequence?) {
        this.rightText = rightText
        binding.btRight.text = rightText
    }

    /**
     * 设置单按钮文本
     * @param centerText 单按钮文本
     */
    fun setButtonTextCenter(centerText: CharSequence?) {
        this.centerText = centerText
        binding.btCenter.text = centerText
    }

    /**
     * 设置左按钮文本颜色
     * @param leftColor 左按钮文本颜色
     */
    fun setButtonTextColorLeft(leftColor: Int) {
        binding.btLeft.setTextColor(leftColor)
    }

    /**
     * 设置右按钮文本颜色
     * @param rightColor 右按钮文本颜色
     */
    fun setButtonTextColorRight(rightColor: Int) {
        binding.btRight.setTextColor(rightColor)
    }

    /**
     * 设置单按钮文本颜色
     * @param centerColor 单按文本颜色
     */
    fun setButtonTextColorCenter(centerColor: Int) {
        binding.btCenter.setTextColor(centerColor)
    }

    /**
     * 设置单按钮是否显示
     * @param  visible true:显示，false:不显示
     */
    fun setCenterButtonVisiblity(visible: Boolean) {
        centerButtonVisiblity = visible
        if (visible) {
            binding.btCenter.visibility = View.VISIBLE
            binding.llLeftRight.visibility = View.GONE
        } else {
            binding.btCenter.visibility = View.GONE
            binding.llLeftRight.visibility = View.VISIBLE
        }
    }

    /**
     * 设置回调
     */
    fun setCallBack(callBack: HintDialogCallBack) {
        this.mCallBack = callBack
    }

    override fun onClick(v: View) {
        dismiss()
        when (v) {
            binding.btLeft -> {
                mCallBack?.hintDialogCallBack(this, ONCLICK_LEFT)
            }
            binding.btRight -> {
                mCallBack?.hintDialogCallBack(this, ONCLICK_RIGHT)
            }
            binding.btCenter -> {
                mCallBack?.hintDialogCallBack(this, ONCLICK_CENTER)
            }
        }
    }


    fun interface HintDialogCallBack {
        /**
         * 点击回调
         * @param dialog  HintDialog实体
         * @param onClick [HintDialog.ONCLICK_LEFT]，[HintDialog.ONCLICK_RIGHT]，[HintDialog.ONCLICK_CENTER]
         */
        fun hintDialogCallBack(dialog: HintDialog, onClick: Int)
    }


}
